package com.dz.risk.common.utils;


import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * Collection工具类
 *
 * @author Doke
 * @date 2020/10/27 13:40
 */
public class ZyCollectionUtils extends CollectionUtils {
    private ZyCollectionUtils() {
    }

    /**
     * 拆分list
     *
     * @param origin 需要拆分的List
     * @param size   拆分的每个容器的长度
     * @param <T>    泛型
     * @return 拆分好的List
     */
    public static <T> List<List<T>> split(final List<T> origin, final int size) {
        if (CollectionUtils.isEmpty(origin)) {
            return Collections.emptyList();
        }
        int block = (origin.size() + size - 1) / size;
        return IntStream.range(0, block).
                boxed().map(i -> {
            int start = i * size;
            int end = Math.min(start + size, origin.size());
            return origin.subList(start, end);
        }).collect(Collectors.toList());
    }
}
